package com.itheima.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

@Controller
@RequestMapping("/check")
public class checkController {

    @RequestMapping("/code")
    public void checkCode(HttpSession session, HttpServletResponse response) throws IOException {
        int width = 100;
        int height =50;
        BufferedImage bf =new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
        Graphics g = bf.getGraphics();

        g.setColor(Color.orange);
        g.fillRect(0,0,width,height);

        g.setColor(Color.cyan);
        g.drawRect(0,0,width-1,height-1);

        String str ="qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890";
        Random r =new Random();

        StringBuilder sb =new StringBuilder();
        g.setColor(Color.blue);
        for (int i = 1; i <=4; i++) {
            int al = r.nextInt(str.length());
            char ch = str.charAt(al);
            sb.append(ch);
            g.drawString(ch+"",width/5*i,height/2);
        }
        session.setAttribute("check",sb.toString());
        ImageIO.write(bf, "jpg", response.getOutputStream());

    }
}
